package com.tengju.bff.interfaces.app.message.assembler;

import com.tengju.bff.interfaces.app.message.vo.MessageDetailVO;
import com.tengju.bff.interfaces.app.message.vo.MyMessageContentVO;
import com.tengju.bff.interfaces.app.message.vo.MyMessageDetailVO;
import com.tengju.bff.interfaces.app.message.vo.MyMessageVO;
import com.tengju.support.domain.model.message.*;
import com.tengju.user.domain.shared.DateUtil;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


public class MessageCentreAssembler {

    public static MyMessageVO toMyMessageVO(List<AppMessageCentreUnreadCount> unreadCountList,
                                            List<AppMessageCentreDetail> appMessageCentreDetailList,
                                            List<Integer> messageTypeList,
                                            Map<Integer,Integer> messageTypeMap) {

        List<MyMessageDetailVO> myMessageDetailVOList = new ArrayList<>();
        MyMessageVO myMessageVO = new MyMessageVO();
        for (Integer type : messageTypeList) {

            MyMessageDetailVO messageDetailVO = new MyMessageDetailVO();

            // 对应消息类型未读消息条数
            AppMessageCentreUnreadCount unreadCount = unreadCountList.stream()
                    .filter(v -> v != null && v.getMessageType() == MessageType.fromVal(type))
                    .findFirst().orElse(null);

            messageDetailVO.setMessageType(type);
            messageDetailVO.setUnreadMsgCount(unreadCount == null ?
                    Long.valueOf(0) : unreadCount.getUnreadMessageCount());
            messageDetailVO.setOrderNum(messageTypeMap.get(type));


            // 对应消息标题、发送时间
            AppMessageCentreDetail centreDetail = appMessageCentreDetailList.stream()
                    .filter(v -> v != null && v.getMessageType() == MessageType.fromVal(type))
                    .findFirst().orElse(null);

            messageDetailVO.setMessageDetailVO(centreDetail == null ?
                    null : new MessageDetailVO(centreDetail.getTitle(), DateUtil.formatLocalDateTime(centreDetail.getSendTime())));


            myMessageDetailVOList.add(messageDetailVO);
        }

        // app-消息中心-首页-消息类型展示顺序
        List<MyMessageDetailVO> orderMyMessageDetailVOList = orderMyMessageDetailVOList(myMessageDetailVOList);

        myMessageVO.setMyMessageDetailVOList(orderMyMessageDetailVOList);

        return myMessageVO;


    }

    public static List<MyMessageContentVO> toMyMessageContentVOList(List<Message> contentList) {
        List<MyMessageContentVO> contentVOList = new ArrayList<>();
        if (CollectionUtils.isEmpty(contentList)) {
            return contentVOList;
        }
        for (Message content : contentList) {
            MyMessageContentVO contentVO = new MyMessageContentVO();
            contentVO.setTitle(content.getTitle() == null ? "" : content.getTitle());
            contentVO.setImgUrl(content.getImgUrl());
            contentVO.setContent(content.getContent());
            contentVO.setJumpUrl(content.getJumpUrl());
            contentVO.setSendTime(DateUtil.toLong(content.getSendTime()));

            contentVOList.add(contentVO);
        }
        return contentVOList;
    }



    /***
     * 后端控制 app-消息中心-首页-消息类型展示顺序
     */
    public static List<MyMessageDetailVO> orderMyMessageDetailVOList(List<MyMessageDetailVO> myMessageDetailVOList) {

        if (CollectionUtils.isEmpty(myMessageDetailVOList)) {
            return new ArrayList<>();
        }
        // 后端控制 app-消息中心-首页-消息类型展示顺序
        myMessageDetailVOList.sort(Comparator.comparing(MyMessageDetailVO::getOrderNum));

        return myMessageDetailVOList;

    }


}
